/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
/**
 * 
 */
package org.myontology.users;

import javax.servlet.http.HttpServletRequest;

import model.Vocabulary;

import org.myontology.controller.AbstractAction;
import org.myontology.controller.AttributeHolder;
import org.myontology.controller.AttributeLevel;
import org.myontology.controller.SesameSession;
import org.myontology.controller.ValueHandler;
import org.myontology.controller.pages.DisplayProperties;
import org.myontology.controller.pages.UserRedirectPage;
import org.openrdf.model.Literal;
import org.openrdf.model.URI;
import org.openrdf.model.impl.LiteralImpl;
import org.openrdf.model.impl.URIImpl;

/**
 * @author <a href="michael.hackl@sti2.at">Michael Hackl</a>
 * Project: Myontology - www.myontology.org
 * STI
 *
 */
public class Login extends AbstractAction {

	public static final String ADMIN = "admin";
	
	
	private SesameSession session;
	public static final ValueHandler<Boolean> LOGGED_IN = new ValueHandler<Boolean>(AttributeLevel.SESSION,Login.class);
	public static final ValueHandler<String> CURRENT_USER = new ValueHandler<String>(AttributeLevel.SESSION,Login.class);
	public static final ValueHandler<String> LAST_PATH = new ValueHandler<String>(AttributeLevel.SESSION,Login.class);
	public static final ValueHandler<String> REDIRECT_PATH = new ValueHandler<String>(AttributeLevel.SESSION,Login.class);
	public static final ValueHandler<String> ROLE = new ValueHandler<String>(AttributeLevel.SESSION,Login.class);
	
	
	public static final ValueHandler<Boolean> SANDBOX = new ValueHandler<Boolean>(AttributeLevel.SESSION,Login.class);
	
	
	public static boolean IS_SANDBOX(HttpServletRequest request) {
		if (SANDBOX.getValue(request)!=null && SANDBOX.getValue(request))
			return true;
		return false;
	}
	
	/* (non-Javadoc)
	 * @see org.myontology.controller.Action#execute(org.myontology.controller.AttributeHolder)
	 */
	public void execute(AttributeHolder attributes) throws Exception {
		DisplayProperties.HIDE_NAVIGATION.setValue(attributes, true);
		DisplayProperties.HIDE_TREE.setValue(attributes, true);
		if ("init".equals(attributes.getRequestParameter("action"))) {
			LOGGED_IN.setValue(attributes, null);
			String path = LAST_PATH.getValue(attributes);
			if (path==null || path.indexOf("logout")>-1)
				path="/";
			REDIRECT_PATH.setValue(attributes, path);
			return;
		}
		String userId = attributes.getRequestParameter("userid");
		URI user = new URIImpl(Vocabulary.USER_NAMESPACE+userId);
		URI hasPwd = Vocabulary.HASPWD;
		Literal pwd = new LiteralImpl(attributes.getRequestParameter("pwd"));
		
//		session.printAllRelations(hasPwd);
		
		boolean successfulLogin = session.hasTriple(user, hasPwd, pwd);
		if (successfulLogin) {
			CURRENT_USER.setValue(attributes, userId);
			if (session.hasTriple(user, Vocabulary.HASLOCK, null)) {
				UserRedirectPage.REDIRECT.setValue(attributes, "/confirm.do?step=init");
			}
			else {
				Literal sessionId = new LiteralImpl(attributes.getSessionId());
				session.update(user, Vocabulary.HASSESSIONID, sessionId);
				LOGGED_IN.setValue(attributes, true);
				if (userId.equals(ADMIN)) {
					ROLE.setValue(attributes, ADMIN);
				}
				else {
					ROLE.setValue(attributes, "user");
				}
				UserRedirectPage.REDIRECT.setValue(attributes, REDIRECT_PATH.getValue(attributes));
			}
		}
		else if (userId==null || userId.trim().length()==0){
			LOGGED_IN.setValue(attributes, null);
			CURRENT_USER.setValue(attributes, null);
		}
		else {
			LOGGED_IN.setValue(attributes, false);
			CURRENT_USER.setValue(attributes, null);
		}
	}

}
